<!--个人中心组件-->
<template>
    <div>
      <el-card class="box-card">
        <div style="margin-top: 40px;">
          <div style="margin-top: 10px; text-align: left"><h1>个人资料</h1></div>
          <div style="border-bottom: #cccccc 1px solid; margin-top: 10px"></div>
          <div style="margin-top: 20px; margin-left: 80px">
            <el-row style="text-align: left;">
              <el-col :span="24" style="margin-top: 10px">
                登录名:&nbsp; <label v-text="Account.userName"></label>
              </el-col>
              <el-col :span="24" style="margin-top: 10px">
                店铺名:&nbsp; <label v-text="Account.shop"></label>
              </el-col>
              <el-col :span="24" style="margin-top: 10px">
                员工管理权限:&nbsp; <label v-text="Account.empPower"></label>
              </el-col>
              <el-col :span="24" style="margin-top: 10px">
                店铺管理权限:&nbsp; <label v-text="Account.chainPower"></label>
              </el-col>
            </el-row>
          </div>
        </div>

        <div style="margin-top: 50px; margin-bottom: 5px">
          <div style="margin-top: 10px; text-align: left"><h1>修改密码</h1></div>
          <div style="border-bottom: #cccccc 1px solid; margin-top: 10px"></div>
          <div style="margin-top: 20px; margin-left: 80px">
            <el-row>
              <el-col :span="10" style="margin-top: 40px; margin-left: 20%">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                  <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </div>
        </div>

      </el-card>
    </div>
</template>

<script>

    export default {
        name: "MyCenterIndex",
      beforeRouteEnter(to, from, next){
        next(vm => {
          vm.Account.userName = localStorage.getItem("userName");
        })
      },
      mounted() {
          this.getAccountInfo();
      },
      data(){
        var validatePass = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入密码'));
          } else {
            if (this.ruleForm.checkPass !== '') {
              this.$refs.ruleForm.validateField('checkPass');
            }
            callback();
          }
        };
        var validatePass2 = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请再次输入密码'));
          } else if (value !== this.ruleForm.pass) {
            callback(new Error('两次输入密码不一致!'));
          } else {
            callback();
          }
        };
        return {
          // 个人中心
          Account: {
            userName: '',
            shop: '',
            empPower: '',
            chainPower: '',
          },

          // 存储密码
          ruleForm: {
            pass: '',
            checkPass: ''
          },
          rules: {
            pass: [
              {validator: validatePass, trigger: 'blur'}
            ],
            checkPass: [
              {validator: validatePass2, trigger: 'blur'}
            ]
          },

        };

      },
      methods: {
          // 获得账户信息
        getAccountInfo(){
          this.axios.post("/api/account-info",{passID: sessionStorage.getItem('passID')}).then((response)=>{
            if(response.data.statusCode == "200"){
              this.Account = JSON.parse(response.data.data);
            }
          })
        },

        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              // 提交checkPass
              this.axios({
                url: '/api/resetpwd',
                method: "post",
                data:{
                  passID: sessionStorage.getItem("passID"),
                  userName: this.Account.userName,
                  pwd: this.ruleForm.checkPass
                },
                headers: {
                  'Content-Type': 'application/json; charset=utf-8'
                }
              }).then((response)=>{
                if(response.data.statusCode == "200"){
                  this.$message({
                    message: '密码修改成功！',
                    center: true,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: '密码修改失败，请稍后重试！',
                    center: true,
                    type: 'error'
                  });
                }
                this.ruleForm= {
                  pass: '',
                  checkPass: ''
                };
              })
            } else {
              this.$message({
                message: '密码修改失败，请稍后重试！',
                center: true,
                type: 'error'
              });
              this.ruleForm= {
                pass: '',
                checkPass: ''
              };
              return false;
            }
          });
        },

        resetForm(formName) {
          this.$refs[formName].resetFields();
        }
      }

    }
</script>

<style scoped>

</style>
